﻿
using System;
using System.Collections;
using Model;
using NHibernate;
using NHibernate.Expression;


namespace DataAccess.Stock
{
    public class StockDataAccess : BaseDataAccess
    {
        public EquipmentOwner GetEquipmentOwner(string codeNumber)
        {
            string query = "FROM EquipmentOwner as equipOwner where equipOwner.CodeNumber = " +codeNumber;
            IList items = m_session.Find(query);
                if (items == null || items.Count < 1)
                    return null;
            return (EquipmentOwner) items[0];
        }
        public EquipPurchaseInfo GetPurchaseInfo(int purchaseId)
        {
            string query = "FROM EquipPurchaseInfo as equipInfo where equipInfo.PurchaseId = " + purchaseId;
            IList items = m_session.Find(query);
            if (items == null || items.Count < 1)
                return null;
            return (EquipPurchaseInfo)items[0];
        }
        public IList GetStockEmployees()
        {
            try
            {
                ICriteria crit = m_session.CreateCriteria(typeof(Employee));
                crit.Add(Expression.Like(Employee.CONST_EMPLOYEE_NAME, "%Stock"));
                crit.AddOrder(Order.Asc(Employee.CONST_EMPLOYEE_NAME));
                return crit.List();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int GetMaxEquipmentCodeNumber()
        {
            string query1 = "select Max(e.CodeNumber) from Equipment as e";
            IList result1 = GetObjects(query1);
            return Int32.Parse(result1[0].ToString());
           
        }
       
        public void SaveEmployee(Employee employee)
        {
            SaveEmployee(employee);
        }

        public IList GetEquipmentWarrantyAlert(int beforeWarranty)
        {
            string query  = " FROM EquipPurchaseInfo as equipInfo INNER JOIN (SELECT equipInfo.PurchaseID, "+
		                    " DateDiff(month, DateAdd(MONTH, ISNULL(equipInfo.WarrantyPeriod, 0), equipInfo.PurchaseDate), GetDate()) NbDiff FROM "+
		                    " equipInfo	WHERE YEAR(equipInfo.PurchaseDate) < 9999) B ON equipInfo.PurchaseID = B.PurchaseID "+
                            " WHERE B.NbDiff <=  " + beforeWarranty + " AND equipInfo.IsWarranty = 1";
            IList items = m_session.Find(query);
            if (items == null || items.Count < 1)
                return null;
            return  items;

        }
        public IList GetEquipmentTypeSystem(int typeId, int systemId)
        {
            ICriteria crit = m_session.CreateCriteria(typeof(EquipmentTypeSystem));
            crit.Add(Expression.Eq(EquipmentTypeSystem.CONST_SYSTEMID, systemId));
            crit.Add(Expression.Eq(EquipmentTypeSystem.CONST_TYPEID, typeId));
            return crit.List();
        }
        public  void DeleteSparepartCompose(string codeNumber)
        {

            string query = "From SparepartComposeEquipment WHERE CodeNumber ='" + codeNumber + "'";
            DeleteObject(query);
        }

        public void DeleteSparepart(int informationId)
        {

           
            string query = "From Sparepart WHERE InformationId =" + informationId + "";
            DeleteObject(query);
        }
        public void DeleteSparepartInfo(int informationId)
        {

            string query = "From SparepartPurchaseInfo WHERE InformationId =" + informationId + "";
            DeleteObject(query);
        }
        public SparepartComposeEquipment GetSparepartComposeEquip(string codeNumber)
        {

            try
            {

                SparepartComposeEquipment equipmentTypeSystem = null;
                ICriteria crit = m_session.CreateCriteria(typeof(SparepartComposeEquipment));
                crit.Add(Expression.Eq(SparepartComposeEquipment.CONST_CODE_NUMBER, codeNumber));
                if (crit.List().Count != 0) equipmentTypeSystem = crit.List()[0] as SparepartComposeEquipment;
                return equipmentTypeSystem;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public Sparepart GetSparepart(string codeNumber)
        {

            try
            {

                Sparepart equipmentTypeSystem = null;
                ICriteria crit = m_session.CreateCriteria(typeof(Sparepart));
                crit.Add(Expression.Eq(SparepartComposeEquipment.CONST_CODE_NUMBER, codeNumber));
                if (crit.List().Count != 0) equipmentTypeSystem = crit.List()[0] as Sparepart;
                return equipmentTypeSystem;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }



    }
}